package com.baizhi.dao;

import com.baizhi.entity.Commit;
import com.baizhi.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CommitDAOImpl implements CommitDAO{
    @Override
    public void addComit(Commit commit) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = "insert into commend (commentid,uid,aid,name,comment,date,title)values (?,?,?,?,?,?,?) ";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,commit.getCommentid());
            pstmt.setInt(2,commit.getUid());
            pstmt.setInt(3,commit.getAid());
            pstmt.setString(4,commit.getName());
            pstmt.setString(5,commit.getComment());
            pstmt.setString(6,commit.getDate());
            pstmt.setString(7,commit.getTitle());
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                pstmt = null;
                if (conn != null) {
                    conn.close();
                }
                conn = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Commit> queryAll(Commit commit) {
        List<Commit> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = "select commentid,uid,aid,name,comment,date,title from commend where title=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,commit.getTitle());
            rs = pstmt.executeQuery();
            Commit c = null;
            while (rs.next()){
                c = new Commit();
                c.setCommentid(rs.getInt(1));
                c.setUid(rs.getInt(2));
                c.setAid(rs.getInt(3));
                c.setName(rs.getString(4));
                c.setComment(rs.getString(5));
                c.setDate(rs.getString(6));
                c.setTitle(rs.getString(7));
                list.add(c);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                rs = null;
                if (pstmt != null) {
                    pstmt.close();
                }
                pstmt = null;
                if (conn != null) {
                    conn.close();
                }
                conn = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
